package com.search.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.search.dao.repo.DoctorRepository;
import com.search.entity.Doctor;
import com.search.service.DoctorQueueSink;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
@EnableBinding(DoctorQueueSink.class)
public class IngestDoctorService {

    @Autowired
    private DoctorRepository doctorRepository;

    private static final String DELETE = "bootstrap-delete";
    private static final String INSERT = "bootstrap-insert";
    private static final String UPDATE = "bootstrap-update";

    @StreamListener(DoctorQueueSink.INPUT)
    public void handle(String content) {
        ObjectMapper om = new ObjectMapper();
        // 解析binlog操作
        String operation = null;
        // 将Json解析成JsonNode
        JsonNode jsonNode = null;

        try {
            operation = om.readTree(content).path("type").asText();
            jsonNode = om.readTree(content).path("data");
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        if (ObjectUtil.isNotEmpty(jsonNode)) {
            Doctor doctor = new Doctor();
            String avatar = ObjectUtil.isNotNull(jsonNode.get("avatar")) ? jsonNode.get("avatar").asText(): "";
            Integer avgResponse = ObjectUtil.isNotNull(jsonNode.get("avg_response")) ?  jsonNode.get("avg_response").asInt(): 0;
            String brief = ObjectUtil.isNotNull(jsonNode.get("brief")) ? jsonNode.get("brief").asText(): "";
            Integer cityId = ObjectUtil.isNotNull(jsonNode.get("city_id")) ?  jsonNode.get("city_id").asInt(): 0;
            String department = ObjectUtil.isNotNull(jsonNode.get("department")) ? jsonNode.get("department").asText(): "";
            Long departmentId = ObjectUtil.isNotNull(jsonNode.get("department_id")) ?  jsonNode.get("department_id").asLong(): 0;
            Long hospitalId = ObjectUtil.isNotNull(jsonNode.get("hospital_id")) ?  jsonNode.get("hospital_id").asLong(): 0;
            String hospital = ObjectUtil.isNotNull(jsonNode.get("hospital")) ? jsonNode.get("hospital").asText(): "";
            String idcard = ObjectUtil.isNotNull(jsonNode.get("idcard")) ? jsonNode.get("idcard").asText(): "";
            String jobtitle = ObjectUtil.isNotNull(jsonNode.get("jobtitle")) ? jsonNode.get("jobtitle").asText(): "";
            Long jobtitleId = ObjectUtil.isNotNull(jsonNode.get("jobtitle_id")) ?  jsonNode.get("jobtitle_id").asLong(): 0;
            BigDecimal minServicePrice = ObjectUtil.isNotNull(jsonNode.get("min_service_price"))
                    ?  new BigDecimal(jsonNode.get("min_service_price").asDouble()): new BigDecimal(0);
            String name = ObjectUtil.isNotNull(jsonNode.get("name")) ? jsonNode.get("name").asText(): "";
            Integer positiveRating = ObjectUtil.isNotNull(jsonNode.get("positive_rating")) ?  jsonNode.get("positive_rating").asInt(): 0;
            Integer serviceNum = ObjectUtil.isNotNull(jsonNode.get("service_num")) ?  jsonNode.get("service_num").asInt(): 0;
            String specially = ObjectUtil.isNotNull(jsonNode.get("specially")) ? jsonNode.get("specially").asText(): "";
            Long userId = ObjectUtil.isNotNull(jsonNode.get("user_id")) ?  jsonNode.get("user_id").asLong(): 0;
            doctor.setAvatar(avatar);
            doctor.setAvgReponse(avgResponse);
            doctor.setBrief(brief);
            doctor.setServiceNum(serviceNum);
            doctor.setDepartment(department);
            doctor.setHospital(hospital);
            doctor.setCityId(cityId);
            doctor.setDepartmentId(departmentId);
            doctor.setName(name);
            doctor.setBrief(brief);
            doctor.setIdcard(idcard);
            doctor.setHospitalId(hospitalId);
            doctor.setMinServicePrice(minServicePrice);
            doctor.setJobtitle(jobtitle);
            doctor.setJobtitleId(jobtitleId);
            doctor.setUserId(userId);
            doctor.setPositiveRating(positiveRating);
            doctor.setSpecially(specially);
            switch (operation) {
                case INSERT:
                case UPDATE:
                    doctorRepository.save(doctor);
                    break;
                case DELETE:
                    doctorRepository.deleteById(doctor.getUserId());
                    break;
                default:
                    break;
            }
        }
    }
}
